/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entityBeans;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
//import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;


/**
 *
 * @author user
 */
@Entity
@Table(name = "comment")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Comment.findAll", query = "SELECT c FROM Comment c"),
    @NamedQuery(name = "Comment.findByCId", query = "SELECT c FROM Comment c WHERE c.cId = :cId"),
    @NamedQuery(name = "Comment.findByText", query = "SELECT c FROM Comment c WHERE c.text = :text")})
public class Comment implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "C_ID")
    private String cId;
//    @Size(max = 40)
    @Column(name = "Text")
    private String text;
    @JoinTable(name = "commented_on", joinColumns = {
        @JoinColumn(name = "C_ID", referencedColumnName = "C_ID")}, inverseJoinColumns = {
        @JoinColumn(name = "Meeting_ID", referencedColumnName = "Meeting_ID")})
    @ManyToOne
    private Helpmeeting helpmeeting;
    @JoinTable(name = "comments", joinColumns = {
        @JoinColumn(name = "C_ID", referencedColumnName = "C_ID")}, inverseJoinColumns = {
        @JoinColumn(name = "User_ID", referencedColumnName = "User_ID")})
    @ManyToOne
    private User user;

    public Comment() {
    }

    public Comment(String cId) {
        this.cId = cId;
    }

    public String getCId() {
        return cId;
    }

    public void setCId(String cId) {
        this.cId = cId;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    @XmlTransient
    public Helpmeeting getHelpmeeting() {
        return helpmeeting;
    }
    
    //This method is written by Ozkan in order to 
    //add a meeting to a comments meeting list
//    public void addHelpMeetingtoCollection(Helpmeeting meeting){
//    	helpmeetingCollection.add(meeting);
//    }
    
    //There is an error with tables because the relationship from meeting entity
    //must be one to many so that we dont need to access any user or meeting from comment
    // we can correct it later but in this case also there is no problem only be careful

    public void setHelpmeeting(Helpmeeting helpmeeting) {
        this.helpmeeting= helpmeeting;
    }
    
    
    ///////////////////////////////////////////////////////////////////
    //
    //	Anwar here there is an example for you. I changed ManyToMany relationship 
    //  to ManyToOne because as you see in the design documentation this must be
    //  a manytoOne relation because one comment can be from only one person for example.
    //  Acoording to this example you can correct all the wrong ManyToMany relations to
    //  ManyToOne relation like I did here
    /////////////////////////////////////////////////////////////////////

    @XmlTransient
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (cId != null ? cId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Comment)) {
            return false;
        }
        Comment other = (Comment) object;
        if ((this.cId == null && other.cId != null) || (this.cId != null && !this.cId.equals(other.cId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "swimtwo.entityBeans.Comment[ cId=" + cId + " ]";
    }
    
}